package com.ljn.springboot.utils;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.ljn.springboot.entity.User;
import com.ljn.springboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Component
public class TokenUtils {
    @Autowired
    private static UserMapper userMapper;

    public static String createToken(String userId, String sign) {
        return JWT.create().withAudience(userId)
                .withExpiresAt(DateUtil.offsetHour(new Date(), 2))
                .sign(Algorithm.HMAC256(sign));
    }

    public static User getCurrentUser() {
        try {
            HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder
                    .getRequestAttributes()).getRequest();
                    String token = request.getHeader("token");
                    if (StrUtil.isNotBlank(token)){
                        String userId = JWT.decode(token).getAudience().get(0);
                        return userMapper.selectById(Integer.valueOf(userId));
                    }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}